package com.example.crad;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.crad.adapter.CardAdapter;
import com.example.crad.vo.Message;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.bt_one)
    Button btOne;
    @BindView(R.id.et_one)
    EditText etOne;
    @BindView(R.id.rv)
    RecyclerView recyclerView;
    List<Message> list;
    String ss = null;
    Message m;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.bt_one)
    public void onViewClicked() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ss = getStr();
                //解析数据
                try {
                    list = new ArrayList<Message>();
                    JSONObject j = new JSONObject(ss);
                    int error_code = j.getInt("error_code");
                    String reason = j.getString("reason");
                    JSONObject result = j.getJSONObject("result");
                    String bankname = result.optString("bankname");
                    String banknum = result.getString("banknum");
                    int cardlength = result.getInt("cardlength");
                    String cardname = result.getString("cardname");
                    String cardprefixnum = result.getString("cardprefixnum");
                    String cardtype = result.getString("cardtype");
                    m = new Message();
                    m.setCardprefixnum(cardprefixnum);
                    m.setBanknum(banknum);
                    m.setCardtype(cardtype);
                    m.setCardname(cardname);
                    m.setCardlength(cardlength);
                    m.setBankname(bankname);
                    list.add(m);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        if (ss == null || ss.isEmpty()) {
            Toast.makeText(this, "111", Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(this, ss, Toast.LENGTH_SHORT).show();
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        recyclerView.setAdapter(new CardAdapter(list));
        Toast.makeText(MainActivity.this, ""+list.size(), Toast.LENGTH_SHORT).show();
    }
    public String getStr() {
        ByteArrayOutputStream byt = new ByteArrayOutputStream();
        InputStream in = null;
        String cardnum = null;
        String s = null;
        try {
            cardnum = URLEncoder.encode(etOne.getText().toString().trim(), "utf-8");
            String key = URLEncoder.encode("f413927d6ff642e9958d06f88d44a68f", "utf-8");
            String path = "http://api.avatardata.cn/Bank/Query?key=" + key + "&cardnum=" + cardnum;
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();//连接
            conn.setRequestMethod("GET");//提交方式
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            byte[] b = new byte[1024];
            if (conn.getResponseCode() == 200) {
                in = conn.getInputStream();
                int len = 0;
                while ((len = in.read(b)) != -1) {
                    byt.write(b, 0, len);
                }
            }
            in.close();
            s = byt.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }

}
